package com.yonyou.pmclouds.altercategory.process;

import com.yonyou.pmclouds.altercategory.entity.AlterCategoryVO;
import com.yonyou.pmclouds.altercategory.mapper.AlterCategoryMapper;
import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.BuziExCodeConst;
import org.apache.commons.lang3.StringUtils;

public class AlterCategoryCodeCheckProcessor implements SingleProcessor<AlterCategoryVO> {
    private AlterCategoryMapper mapper;

    public AlterCategoryCodeCheckProcessor(AlterCategoryMapper mapper) {
        this.mapper = mapper;
    }

    @Override
    public void process(AlterCategoryVO vo, AlterCategoryVO orig) throws BusinessException {
        // 校验级次正确性
        String pkParent = vo.getPkParent();
        String pkRoot = vo.getPkRoot();
        if (StringUtils.isAllEmpty(pkRoot, pkParent)) {
            // 两个全空，说明是根类别
            vo.setPkRoot(vo.getPkAltercategory());
            checkUnique(vo);
            return;
        }
        AlterCategoryVO parent = null;
        if (!StringUtils.isEmpty(pkParent)) {
            parent = mapper.getById(pkParent);
            if (parent != null && parent.getDr() != 0) {
                parent = null;
            }
        }
        if (parent == null) {
            throw new BusinessException("无法获取要新增类别的上级类别，请确认上级类别是否存在!");
        }
        AlterCategoryVO root = null;
        if (!StringUtils.isEmpty(pkRoot)) {
            if (StringUtils.equals(pkParent, pkRoot)) {
                root = parent;
            } else {
                root = mapper.getById(pkRoot);
            }
        } else {
            // 不排除前端取不到root的结果，
            pkRoot = parent.getPkRoot();
            if (StringUtils.equals(pkRoot, pkParent)) {
                root = parent;
            } else {
                root = mapper.getById(pkRoot);
            }
        }
        if (root == null) {
            throw new BusinessException("无法获取要新增类别的根类别，请确认根类别是否存在！");
        }

        if (!pkParent.equals(pkRoot)) {
            throw new BusinessException("新增的类别不可超过二级！");
        }
        vo.setPkRoot(pkRoot);
        // 校验父编码一致性
        String parentCode = parent.getScode();
        if (!vo.getScode().startsWith(parentCode)) {
            throw new BusinessException("新增类别的编码必须以上级类别的编码起始！");
        }
        checkUnique(vo);

    }

    private void checkUnique(AlterCategoryVO vo) throws BusinessException {
        // 校验编码的唯一性
        boolean codeUnique = mapper.countCode(vo.getPkTenant(), vo.getScode()) == 0;
        boolean nameUnique = mapper.countName(vo.getPkTenant(), vo.getSname()) == 0;
        if (!codeUnique && !nameUnique) {
            throw new BusinessException("已经存在相同的编码、名称，请修改!", BuziExCodeConst.CODE_NAME_CODE_DUPLICATE);
        }
        if (!codeUnique) {
            throw new BusinessException("已经存在相同的编码，请修改!", BuziExCodeConst.CODE_CODE_DUPLICATE);
        }
        if (!nameUnique) {
            throw new BusinessException("已经存在相同的名称，请修改!", BuziExCodeConst.CODE_NAME_DUPLICATE);
        }
    }
}
